package edu.cmu.ibill.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.dao.DAOException;

import edu.cmu.ibill.databeans.User;
import edu.cmu.ibill.model.*;

public class ChangePasswordAction extends Action {

	UserDAO userDAO;

	public ChangePasswordAction(Model model) {
		this.userDAO = model.getUserDAO();
	}

	@Override
	public String getName() {
		return "changepassword.do";
	}

	@Override
	public String perform(HttpServletRequest request) {

		List<String> errors = new ArrayList<String>();
		request.setAttribute("addpersonalbillerrors", errors);
		User user = (User) request.getSession(false).getAttribute("user");

		String password = request.getParameter("newpassword");

		user.setPassword(password);

		try {
			userDAO.update(user);
		} catch (DAOException e) {
			errors.add(e.getMessage());
		}

		return "setting.jsp";
	}

}
